
function [ ReMask_map,ref_map ] = generaterefferencemap( file_path,t1_t2 )
    
load( file_path );
slice_num = 2;
if( size(T1,3) < 2 )
    slice_num = 1 ;
end
T1_map = squeeze( T1(:,:,slice_num,t1_t2));
T1_map(isnan(T1_map)) = 0;
T1_map(T1_map>300) = 0;
%T1_map = fliplr(T1_map);
T1_map(T1_map>0)= abs((T1_map(T1_map>0)-86));
Bineary_map = zeros( size(T1_map));
%for weight
T1_map(T1_map>3000) = 0;
T1_map(T1_map<((t1_t2==1)*100+(t1_t2==2)*15)) = 0;

 


Bineary_map(T1_map>0) = 1;
 SE=strel('square',2);
 Bineary_map=imerode(Bineary_map,SE);
 
Label_Bineary_map = zeros( size(T1_map));
[Label_Bineary_map, object_num] = bwlabel(Bineary_map,4);
Theta_arr = linspace(0 , 2*pi , 360);

valid_area_flg = zeros( 1,  object_num);
Maskmap = zeros( size(T1_map,1),size(T1_map,2),  object_num);
for object_ix = 1: object_num
    Temp_map =  zeros( size(T1_map));
    Temp_map(Label_Bineary_map==object_ix) = 1;
    [row_iy, col_ix,value]=find(Temp_map>0);
    area_size = sum( sum( Temp_map ));
    if( area_size >40 )
        valid_area_flg( 1,  object_ix ) = 1;
        Center_iy = mean( row_iy );
        Center_ix = mean( col_ix );
        Row_len = max( row_iy  ) - min( row_iy );
        Col_len = max( col_ix ) - min( col_ix );
        R = min( [Row_len Col_len ])*0.4;    %RuiGuo changing to this value is 0.3 for MS T1T2
        Y_arr = R*sin(Theta_arr)+ Center_iy ;
        X_arr = R*cos(Theta_arr) + Center_ix ;

        %mesh grid 
        [mesh_x,mesh_y] = meshgrid([1:size(T1_map,2)],[1:size(T1_map,1)]);
        obj_area = inpolygon(mesh_x,mesh_y,X_arr,Y_arr);
         Maskmap(:,:,object_ix) = obj_area;
        [plant_i,plant_j] = find(obj_area);
        obj_area = obj_area.*T1_map ; 
      
    end
end 
ReMask_map = zeros( size(T1_map,1),size(T1_map,2),  sum( valid_area_flg));
valid_num  =1 ;
for tag_ix = 1 : object_num
    if( valid_area_flg( 1,tag_ix) == 1)
        ReMask_map(:,:,valid_num) = Maskmap(:,:,tag_ix);
        valid_num = valid_num + 1;
    end
end
ref_map = T1_map ;
end